<template>
  <Dialog :title="t('judgeScene.final.finalList')" v-model="dialogVisible" width="1200">
    <ContentWrap>
      <el-button type="success" plain @click="handleExport" :loading="exportLoading">
        <Icon icon="ep:download" class="mr-5px" />
        {{ t('action.export') }}
      </el-button>
    </ContentWrap>
    <ContentWrap>
      <vxe-table
        id="iFinalWin-table"
        v-loading="loading"
        :data="list"
        stripe
        :custom-config="vxeTableConfig"
        :column-config="{ resizable: true }"
        :size="vxeTableSize"
      >
        <vxe-column :title="t('common.index')" type="seq" field="seq" width="60" align="center" />
        <vxe-column
          :title="t('candidate.fields.jobCode')"
          field="jobCode"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('candidate.fields.studentName')"
          field="studentName"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('candidate.fields.major')"
          field="major"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('judgeScene.initial.awardLevel')"
          field="awardLevel"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('judgeScene.initial.initialScore')"
          field="initialScore"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('judgeScene.initial.finalScore')"
          field="finalScore"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('judgeScene.initial.sameAmount')"
          field="sameAmount"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('judgeScene.initial.sort')"
          field="sort"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        />
        <vxe-column
          :title="t('judgeScene.initial.isWin')"
          field="isWin"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        >
          <template #default="scope">
            <span v-if="scope.row.isWin">{{ t('common.yes') }}</span>
            <span v-else>{{ t('common.no') }}</span>
          </template>
        </vxe-column>
        <vxe-column
          :title="t('judgeScene.initial.differenceSelection')"
          field="differenceSelection"
          align="center"
          show-header-overflow
          show-overflow
          min-width="100"
        >
          <template #default="scope">
            <span v-if="scope.row.differenceSelection">{{ t('common.yes') }}</span>
            <span v-else>{{ t('common.no') }}</span>
          </template>
        </vxe-column>
      </vxe-table>
      <!-- 分页 -->
      <Pagination
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </ContentWrap>
  </Dialog>
</template>

<script setup lang="ts">
import { FinalWinApi } from '@/api/awards/finalwin'
import type { VxeTablePropTypes } from 'vxe-table'
import { useAppStore } from '@/store/modules/app'
import { StudentAwardResultResVO } from '@/api/awards/reviewscene'
import download from '@/utils/download'

/** 最终获奖记录 列表 */
defineOptions({ name: 'FinalWin' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const dialogVisible = ref(false)
const loading = ref(true) // 列表的加载中
const list = ref<StudentAwardResultResVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  awardsId: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const vxeTableConfig: VxeTablePropTypes.CustomConfig = {
  storage: true,
  mode: 'drawer',
  checkMethod({ column }) {
    return !['seq', 'operator'].includes(column.field)
  }
}

const appStore = useAppStore()
const vxeTableSize = computed(() => appStore.getCurrentSizeVxe)

/** 查询列表 */
const getList = async () => {
  queryParams.awardsId = awardsId.value
  loading.value = true
  try {
    const data = await FinalWinApi.getFinalWinPageVo(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    queryParams.awardsId = awardsId.value
    // 发起导出
    exportLoading.value = true
    const data = await FinalWinApi.exportFinalWin(queryParams)
    download.excel(data, '最终获奖记录.xlsx')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

const awardsId = ref('')
const open = (_awardsId: string) => {
  reset()
  awardsId.value = _awardsId
  dialogVisible.value = true
  getList()
}

const reset = () => {
  queryParams.pageNo = 1
  queryParams.pageSize = 10
  list.value = []
  total.value = 0
}

defineExpose({ open })
</script>
